var _questionId = null;
var _problem = [];
var _openId = null;
var _flag = 0;
function Problem(problemId, type) {
	this.problemId = problemId;
	this.type = type;
};
$(function() {
	qmsk.get('../questionnaire/currentId', function(response) {
		var questionId = response.getValue('questionId');
		if (qmsk.isNotEmpty(questionId)) {
			_questionId = questionId;
			_openId = wx.getOpenId();
			loadData();
			if (qmsk.isNotEmpty(_openId))
				loadBaseInfo(_openId);
		} else {
			document.write("当前没有问卷调查！");
		}
	});

});

function loadBaseInfo(openId) {
	qmsk.get('../cust/find/baseinfo', {
		openId : openId
	}, function(response) {
		var customerName = response.getValue("customerName");
		var mp = response.getValue("mp");
		$("#name").val(customerName);
		$("#tel").val(mp);
	});
}

function loadData() {
	qmsk.get('../questionnaire/page/' + _questionId, function(response) {
		//"problemId","content","type","typeName","answerId","answercontent"

		_problem = [];
		$("#problemPanel").empty();
		var row = response.row;
		var $problemHtml = null;
		for (var i = 0; i < row; i++) {
			var problemId = response.getValue('problemId', i);
			var content = response.getValue('content', i);
			var type = response.getValue('type', i);
			var j = 0;
			for (; j < _problem.length; j++) {
				if (_problem[j].problemId == problemId) {
					break;
				}
			}
			if (j == _problem.length) {
				$problemHtml = $("<p class='mainTxt'>" + (_problem.length + 1) + "、" + content + "<br/></p>");
				var problem = new Problem(problemId, type);
				_problem.push(problem);
				$("#problemPanel").append($problemHtml);

				if (type == 3) {
					$selectHtml = $("<select  id='p_" + problemId + "' class='way'></select>");
					$problemHtml.append($selectHtml);
					$problemHtml = $selectHtml;
				}

			}

			if (type == 4) {
				var $html = $("<textarea id='p_" + problemId + "' class='noSurvice'></textarea>");
				$problemHtml.append($html);
				continue;
			}
			//"problemId","content","type","typeName","answerId","answercontent"
			var answerId = response.getValue('answerId', i);
			var answercontent = response.getValue('answercontent', i);

			if (type == 1) {
				var $html = $("<label  class='check'><input value='" + answerId + "' name='p_" + problemId + "' type='radio' name='sweep'>" + answercontent + "</label>");
				$problemHtml.append($html);
				continue;
			}

			if (type == 2) {
				var $html = $("<label class='check'><input value='" + answerId + "' name='p_" + problemId + "' type='checkbox' name='sweep'>" + answercontent + "</label>");
				$problemHtml.append($html);
				continue;
			}

			if (type == 3) {
				var $html = $("<option value='" + answerId + "'>" + answercontent + "</option>");
				$problemHtml.append($html);
				continue;
			}

		}
	});
}

function submit() {
	if (_flag == 1) {
		alert("您已经提交！");
		return;
	}
	var data = new Object();
	data.name = $("#name").val();
	data.tel = $("#tel").val();
	data.openId = _openId;
	for (var i = 0; i < _problem.length; i++) {
		var problemId = _problem[i].problemId;
		var type = _problem[i].type;
		if (type == 1) {
			data['' + problemId] = $("input[name='p_" + problemId + "']:checked").val();
		} else if (type == 2) {
			var elements = $("input[name='p_" + problemId + "']:checked");
			var value = [];
			for (var j = 0; j < elements.length; j++) {
				var v = $(elements[j]).val();
				value.push(v);
			}
			data['' + problemId] = value.join(',');
		} else {
			data['' + problemId] = $("#p_" + problemId).val();
		}
	}
	qmsk.post('../questionnaire/submit/' + _questionId, data, function(response) {
		_flag = 1;
		alert('提交成功');
	});
}